﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using KingRoad.EPM.CompletionMaterialsEntity.Model;
using KingRoad.EPM.Web.Areas.CompletionMaterials.ViewModels;
using KingRoad.EPM.ProjectInfoEntity;
using Unis.Framework.Core.Utility;

namespace KingRoad.EPM.Web.Areas.CompletionMaterials.Controllers
{
    public class ProjectSegmentController : BaseController
    {

        /// <summary>
        /// 施工单位
        /// </summary>
        /// <returns></returns>
        public ActionResult UnitList(string uId, string uName, string uSection)
        {
            int type = 0;
            switch (GetLoginUserOrganizeEntity.Code)
            {
                case "SG":
                    type = 1;
                    break;
                case "JL":
                    type = 2;
                    break;
                case "ZDJL":
                    type = 3;
                    break;
                case "JC":
                    type = 4;
                    break;
            }
            ViewBag.unit_type = type;
            ViewBag.uId = uId;
            ViewBag.uName = uName;
            ViewBag.uSection = uSection;

            //当前页码
            ViewBag.pageNum = 1;
            //每页多少条
            ViewBag.numPerPage = 10;

            List<KingRoad.EPM.ProjectInfoEntity.PROJECT_SEGMENT> seglist = new List<KingRoad.EPM.ProjectInfoEntity.PROJECT_SEGMENT>();
            switch (type)
            {
                case 1:
                    seglist = ShiGong_Segment;
                    break;
                case 2:
                    seglist = GetJLProjectSegmentInfo;
                    break;
                case 3:
                    seglist = GetZDJLProjectSegmentInfo;
                    break;
                case 4:
                    seglist = GetJCProjectSegmentInfo;
                    break;
                default:
                    seglist = ShiGong_Segment;
                    break;
            }
            var totalRecord = seglist.Count;
            var kingRoadsegList = ProjectSegmentUIRule.GetProjectSegmentListForPage<KingRoad.EPM.ProjectInfoEntity.PROJECT_SEGMENT>(
                 ViewBag.pageNum, ViewBag.numPerPage, "SEGMENT_ID", seglist);
            var segmentList = new List<ProjectSegmentInfo>();
            foreach (var item in kingRoadsegList)
            {
                var projectSegment = new ProjectSegmentInfo();
                var unisPros = typeof(ProjectSegmentInfo).GetProperties().ToList();

                unisPros.ForEach(t =>
                {

                    var kingRoadPro = item.GetType().GetProperty(t.Name);

                    if (kingRoadPro != null)
                    {
                        var value = kingRoadPro.GetValue(item, null);


                        t.SetValue(projectSegment, value, null);
                    }
                });
                segmentList.Add(projectSegment);
            }


            //总条数
            ViewBag.totalcount = segmentList.Count;
            //总页数
            ViewBag.pagenumshown = totalRecord % ViewBag.numPerPage == 0 ? totalRecord / ViewBag.numPerPage : totalRecord / ViewBag.numPerPage + 1;

            return View(new ProjectunitViewModel { TheSegmentList = segmentList });
        }
        /// <summary>
        /// 施工单位(分页提交)
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult UnitSplitPage()
        {
            ViewBag.unit_type = Request.Form["unit_type"];
            int type = Convert.ToInt32(ViewBag.unit_type);
            ViewBag.uId = Request.Form["uId"];
            ViewBag.uName = Request.Form["uName"];
            ViewBag.uSection = Request.Form["uSection"];
            int totalRecord = 0;
            //当前页码
            ViewBag.pageNum = Convert.ToInt32(Request.Form["pageNum"]);
            //每页多少条

            ViewBag.numPerPage = Convert.ToInt32(Request.Form["numPerPage"]);
            ViewBag.iput_deName = Request.Form["iput_deName"];

            List<KingRoad.EPM.ProjectInfoEntity.PROJECT_SEGMENT> seglist = new List<KingRoad.EPM.ProjectInfoEntity.PROJECT_SEGMENT>();
            switch (type)
            {
                case 1:
                    seglist = ShiGong_Segment;
                    break;
                case 2:
                    seglist = GetJLProjectSegmentInfo;
                    break;
                case 3:
                    seglist = GetZDJLProjectSegmentInfo;
                    break;
                case 4:
                    seglist = GetJCProjectSegmentInfo;
                    break;
                default:
                    seglist = ShiGong_Segment;
                    break;
            }
            if (!String.IsNullOrEmpty(ViewBag.iput_deName))
                seglist = seglist.Where(t => t.SEGMENT_DEPARTNAME.Contains(ViewBag.iput_deName)).ToList();

            List<KingRoad.EPM.ProjectInfoEntity.PROJECT_SEGMENT> kingRoadsegList = ProjectSegmentUIRule.GetProjectSegmentListForPage<KingRoad.EPM.ProjectInfoEntity.PROJECT_SEGMENT>(
                ViewBag.pageNum, ViewBag.numPerPage, "SEGMENT_ID", seglist);
            var segmentList = kingRoadsegList.CopyDataTo<PROJECT_SEGMENT, ProjectSegmentInfo>();
            totalRecord = seglist.Count;
            //总条数
            ViewBag.totalcount = totalRecord;
            //总页数
            ViewBag.pagenumshown = totalRecord % ViewBag.numPerPage == 0 ? totalRecord / ViewBag.numPerPage : totalRecord / ViewBag.numPerPage + 1;
            return View("UnitList", new ProjectunitViewModel { TheSegmentList = segmentList });
        }




        /// <summary>
        /// 根据施工标段类型获取施工标段信息
        /// </summary>
        /// <param name="type">施工标段类型（1=土建，2=路面，3=交安，4=绿化，5=房建，6=机电，7=污水）</param>
        /// <param name="url">树节点链接地址</param>
        /// <param name="ntype">节点名的长度(short=标段，long=标段+施工单位名)</param>
        /// <returns></returns>
        public List<treeNodes> GetSegmentNodesListByType(int type, string url, string ntype)
        {
            List<treeNodes> treenodes = new List<treeNodes>();
            //TODO：没有按类型获取施工单位的接口，暂时将施工单位汇总在一起
            if (type == 1)
            {
                //TODO:根据当前登录人 并根据单位类型 获取所管辖施工标段信息
                List<KingRoad.EPM.ProjectInfoEntity.PROJECT_SEGMENT> sionList = ShiGong_Segment;
                foreach (KingRoad.EPM.ProjectInfoEntity.PROJECT_SEGMENT info in sionList)
                {
                    treeNodes node = new treeNodes();
                    node.id = info.SEGMENT_CODE;
                    node.pId = type.ToString();
                    if (ntype == "short")
                        node.name = info.SEGMENT_CODE;
                    if (ntype == "long")
                        node.name = "(" + info.SEGMENT_CODE + ")" + info.SEGMENT_DEPARTNAME;
                    if (url != null && url != "")
                    {
                        node.target = "ajax";
                        node.url = url + "?Section=" + info.SEGMENT_CODE;
                    }
                    treenodes.Add(node);
                }
            }
            return treenodes;
        }

        /// <summary>
        /// 总监办标段信息
        /// </summary>
        /// <param name="url">树节点链接地址</param>
        /// <param name="ntype">节点名的长度(short=标段，long=标段+总监办单位名)</param>
        /// <returns></returns>
        public List<treeNodes> GetSionNodesList(string url, string ntype)
        {
            List<treeNodes> treenodes = new List<treeNodes>();
            //TODO:根据当前登录人获取所管辖 总监 监理标段信息
            List<KingRoad.EPM.ProjectInfoEntity.PROJECT_SEGMENT> sionList = GetJLProjectSegmentInfo;
            foreach (KingRoad.EPM.ProjectInfoEntity.PROJECT_SEGMENT info in sionList)
            {
                treeNodes node = new treeNodes();
                node.id = info.SEGMENT_CODE;
                node.pId = "10";
                if (ntype == "short")
                    node.name = info.SEGMENT_CODE;
                if (ntype == "long")
                    node.name = "(" + info.SEGMENT_CODE + ")" + info.SEGMENT_DEPARTNAME;
                if (url != null && url != "")
                {
                    node.target = "ajax";
                    node.url = url + "?Section=" + info.SEGMENT_CODE + "&type=1";
                }
                treenodes.Add(node);
            }
            return treenodes;
        }
        /// <summary>
        /// 驻地标段信息
        /// </summary>
        /// <param name="url">树节点链接地址</param>
        /// <param name="ntype">节点名的长度(short=标段，long=标段+驻地办单位名)</param>
        /// <returns></returns>
        public List<treeNodes> GetSingNodesList(string url, string ntype)
        {
            List<treeNodes> treenodes = new List<treeNodes>();
            //TODO:根据当前登录人获取所管辖 驻地 监理标段信息
            List<KingRoad.EPM.ProjectInfoEntity.PROJECT_SEGMENT> sionList = GetZDJLProjectSegmentInfo;
            foreach (KingRoad.EPM.ProjectInfoEntity.PROJECT_SEGMENT info in sionList)
            {
                treeNodes node = new treeNodes();
                node.id = info.SEGMENT_CODE;
                node.pId = "10";
                if (ntype == "short")
                    node.name = info.SEGMENT_CODE;
                if (ntype == "long")
                    node.name = "(" + info.SEGMENT_CODE + ")" + info.SEGMENT_DEPARTNAME;
                if (url != null && url != "")
                {
                    node.target = "ajax";
                    node.url = url + "?Section=" + info.SEGMENT_CODE + "&type=2";
                }
                treenodes.Add(node);
            }
            return treenodes;
        }
        /// <summary>
        /// 检测标段信息
        /// </summary>
        /// <param name="url">树节点链接地址</param>
        /// <param name="ntype">节点名的长度(short=标段，long=标段+检测单位名)</param>
        /// <returns></returns>
        public List<treeNodes> GetVerifyNodesList(string url, string ntype)
        {
            List<treeNodes> treenodes = new List<treeNodes>();
            //TODO:根据当前登录人获取所管辖 检查 标段信息
            List<KingRoad.EPM.ProjectInfoEntity.PROJECT_SEGMENT> sionList = GetJCProjectSegmentInfo;
            foreach (KingRoad.EPM.ProjectInfoEntity.PROJECT_SEGMENT info in sionList)
            {
                treeNodes node = new treeNodes();
                node.id = info.SEGMENT_CODE;
                node.pId = "10";
                if (ntype == "short")
                    node.name = info.SEGMENT_CODE;
                if (ntype == "long")
                    node.name = "(" + info.SEGMENT_CODE + ")" + info.SEGMENT_DEPARTNAME;
                if (url != null && url != "")
                {
                    node.target = "ajax";
                    node.url = url + "?Section=" + info.SEGMENT_CODE + "&type=3";
                }
                treenodes.Add(node);
            }
            return treenodes;
        }

        /// <summary>
        /// 获取施工标段Json信息
        /// </summary>
        /// <returns></returns>
        public JsonResult GetSegmentJson()
        {
            string url = Request.QueryString["url"];
            List<ScmSafetyconstructiondicInfo> typeList = ScmSafetyconstructiondicUIRule.GetSafetyconstructiondicByPID("AF53BE17-B616-4C17-98C7-1E8A88001E34");
            List<treeNodes> treenodes = new List<treeNodes>();
            foreach (ScmSafetyconstructiondicInfo tInfo in typeList)
            {
                //施工标段信息
                List<treeNodes> nodesList = GetSegmentNodesListByType(Convert.ToInt32(tInfo.Scd_Code), url, "long");
                if (nodesList.Count > 0)
                {
                    treeNodes pnode = new treeNodes();
                    pnode.id = tInfo.Scd_Code;
                    pnode.pId = "0";
                    pnode.name = "施工单位";// tInfo.Scd_Name;
                    treenodes.Add(pnode);
                    treenodes.AddRange(nodesList);
                }
            }
            return Json(treenodes, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 获取总监标段Json信息
        /// </summary>
        /// <returns></returns>
        public JsonResult GetSionJson()
        {
            string url = Request.QueryString["url"];
            List<treeNodes> treenodes = new List<treeNodes>();
            //总监办 监理标段信息
            List<treeNodes> sionNodesList = GetSionNodesList(url, "short");
            if (sionNodesList.Count > 0)
            {
                treeNodes jnode = new treeNodes();
                jnode.id = "10";
                jnode.pId = "0";
                jnode.name = "总监办";
                treenodes.Add(jnode);
                if (sionNodesList.Count > 0)
                    treenodes.AddRange(sionNodesList);
            }
            return Json(treenodes, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 获取监理驻地标段Json信息
        /// </summary>
        /// <returns></returns>
        public JsonResult GetSingJson()
        {
            string url = Request.QueryString["url"];
            List<treeNodes> treenodes = new List<treeNodes>();
            //驻地 监理标段信息
            List<treeNodes> singNodesList = GetSingNodesList(url, "short");
            if (singNodesList.Count > 0)
            {
                treeNodes jnode = new treeNodes();
                jnode.id = "10";
                jnode.pId = "0";
                jnode.name = "驻地办";
                treenodes.Add(jnode);
                if (singNodesList.Count > 0)
                    treenodes.AddRange(singNodesList);
            }
            return Json(treenodes, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 获取监理检测标段Json信息
        /// </summary>
        /// <returns></returns>
        public JsonResult GetVeriJson()
        {
            string url = Request.QueryString["url"];
            List<treeNodes> treenodes = new List<treeNodes>();
            //检查 标段信息
            List<treeNodes> veriNodesList = GetVerifyNodesList(url, "short");
            if (veriNodesList.Count > 0)
            {
                treeNodes jnode = new treeNodes();
                jnode.id = "10";
                jnode.pId = "0";
                jnode.name = "中心试验室";
                treenodes.Add(jnode);
                if (veriNodesList.Count > 0)
                    treenodes.AddRange(veriNodesList);
            }
            return Json(treenodes, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 获取监理(总监+驻地+检测)标段Json信息
        /// </summary>
        /// <returns></returns>
        public JsonResult GetSupervJson()
        {
            List<treeNodes> treenodes = new List<treeNodes>();
            //总监办 监理标段信息
            List<treeNodes> sionNodesList = GetSionNodesList("", "long");
            //驻地 监理标段信息
            List<treeNodes> singNodesList = GetSingNodesList("", "long");
            //检查 标段信息
            List<treeNodes> veriNodesList = GetVerifyNodesList("", "long");
            if (sionNodesList.Count > 0 || singNodesList.Count > 0 || veriNodesList.Count > 0)
            {
                treeNodes jnode = new treeNodes();
                jnode.id = "10";
                jnode.pId = "0";
                jnode.name = "监理单位";
                treenodes.Add(jnode);
                if (sionNodesList.Count > 0)
                    treenodes.AddRange(sionNodesList);
                if (singNodesList.Count > 0)
                    treenodes.AddRange(singNodesList);
                if (veriNodesList.Count > 0)
                    treenodes.AddRange(veriNodesList);
            }
            return Json(treenodes, JsonRequestBehavior.AllowGet);
        }


        /// <summary>
        ///  获取所有标段（监理+施工）Json信息
        /// </summary>
        /// <returns></returns>
        public JsonResult GetAllSegmentJson()
        {
            string codes = Request.QueryString["codes"];
            List<treeNodes> treenodes = new List<treeNodes>();

            //总监办 监理标段信息
            List<treeNodes> sionNodesList = GetSionNodesList("", "long");
            //驻地 监理标段信息
            List<treeNodes> singNodesList = GetSingNodesList("", "long");
            //检查 标段信息
            List<treeNodes> veriNodesList = GetVerifyNodesList("", "long");
            if (sionNodesList.Count > 0 || singNodesList.Count > 0 || veriNodesList.Count > 0)
            {
                treeNodes jnode = new treeNodes();
                jnode.id = "10";
                jnode.pId = "0";
                jnode.name = "监理单位";
                treenodes.Add(jnode);
                if (sionNodesList.Count > 0)
                    treenodes.AddRange(sionNodesList);
                if (singNodesList.Count > 0)
                    treenodes.AddRange(singNodesList);
                if (veriNodesList.Count > 0)
                    treenodes.AddRange(veriNodesList);
            }
            List<ScmSafetyconstructiondicInfo> typeList = ScmSafetyconstructiondicUIRule.GetSafetyconstructiondicByPID("AF53BE17-B616-4C17-98C7-1E8A88001E34");
            foreach (ScmSafetyconstructiondicInfo tInfo in typeList)
            {
                //施工标段信息
                List<treeNodes> nodesList = GetSegmentNodesListByType(Convert.ToInt32(tInfo.Scd_Code), "", "long");
                if (nodesList.Count > 0)
                {
                    treeNodes pnode = new treeNodes();
                    pnode.id = tInfo.Scd_Code;
                    pnode.pId = "0";
                    pnode.name = "施工单位";// tInfo.Scd_Name;
                    treenodes.Add(pnode);
                    treenodes.AddRange(nodesList);
                }
            }
            return Json(treenodes, JsonRequestBehavior.AllowGet);
        }



    }
}
